<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../css/ui-base.css" />
    <link rel="stylesheet" href="../css/goodsDetail.css" />
    <link rel="stylesheet" href="../css/detail.css" />
    <script src="../js/jquery-3.6.0.min.js"></script>
    <script src="../js/interface.js"></script>
    <script src="../js/cookie.js"></script>
  </head>

  <body>
    <!-- 引入公共头部 -->
    <div class="header"></div>
    <script type="text/javascript">
      $(".header").load("common/head.html", function () {
        var cookie = document.cookie;
        //记录用户
        if (cookie) {
          var username = getCookie("lgc");
          if (username) {
            $(".tipsBar").html(
              `<span>欢迎您,${username} | </span><span onclick="exit()">退出</span>`
            );
          }
        }
      });
    </script>
    <!-- 主体 -->
    <div class="wrapAll cl">
      <div class="left">
        <div class="showGoods">
          <img
            class="smallImg"
            src="../images/x1630565864973_8163_x_448_x_448.jpg"
            alt=""
          />
          <div class="shadow"></div>
        </div>
        <ul class="showList">
          <li class="active">
            <img
              src="../images/x1630565864973_8163_x_448_x_448.jpg"
              bigUrl="../images/x1630565864973_8163_x_800_x_800.jpg"
              alt=""
              class="smImg"
            />
          </li>
        </ul>
      </div>
      <div class="right">
        <div class="scaleGoods">
          <img
            class="bigImg"
            src="../images/x1630565864973_8163_x_800_x_800.jpg"
            alt=""
          />
        </div>

        <div class="goodsInfo">
          <div class="title">
            【吴磊同款】匹克中国力量秋季新款男子篮球背心DF713001
          </div>
          <div class="detail cl">
            <div>
              <span>领&emsp;&emsp;券</span>
            </div>

            <div class="activity cl">
              <span>活&emsp;&emsp;动</span>
              <i>积分抵现</i>
              <i>此商品匹克积分最高可抵现40元。 </i>
              <i>积分抵现规则详情>> </i>
            </div>
            <div class="activity cl">
              <span>&emsp;&emsp;&emsp;&emsp;</span>
              <i>满减包邮</i>
              <i>【订单满59元】 包邮 </i>
            </div>

            <!-- 尺码 -->
            <!-- <div class="color cl">
              <p>选择颜色</p>
              <li>
                <img
                  src="../images/x1622084912003_7410_x_448_x_448.jpg"
                  alt=""
                />
              </li>
              <li>
                <img
                  src="../images/x1622084912003_2843_x_448_x_448.jpg"
                  alt=""
                />
              </li>
              <li>
                <img
                  src="../images/x1622084912003_3600_x_448_x_448.jpg"
                  alt=""
                />
              </li>
            </div> -->

            <div class="size cl">
              <p>选择尺寸</p>
              <li>S</li>
              <li>M</li>
              <li>L</li>
              <li>XL</li>
              <li>X2L</li>
              <li>X3L</li>
              <li>X4L</li>
            </div>

            <div class="num cl">
              <p>购买数量</p>
              <div class="count cl">
                <div class="reduce">
                  <a href="javascript:;">-</a>
                </div>
                <input type="text" class="count-input" value="1" />
                <div class="add">
                  <a href="javascript:;">+</a>
                </div>
              </div>
              <p>&emsp;件&nbsp;（库存99+件）</p>
            </div>
            <p>已经选择商品:</p>
            <div class="buyGoods">
              <div class="addToShoppingCar">
                <img src="../images/shoppingcart.png" alt="" />
                <span>加入购物车</span>
              </div>
              <div class="heart">
                <img src="../images/heart.png" alt="" />
                <span>喜欢</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="goodsDetail"></div>
    <footer></footer>
    <script>
      $("footer").load(`common/footer.html`);
    </script>
  </body>
  <script>
    var cookie = document.cookie;
    var pid;
    if (cookie) {
      var username = getCookie("lgc");
    }
    console.log(username);

    function exit() {
      deleteCookie("lgc");
      location.reload();
    }
    $(async function () {
      $(document).on("click", ".searchBtn", function () {
        type = $(".searchCon").val();
        window.location.href = `./goodsList.html?type=${type}`;
      });
      function urlParse() {
        var search = location.search;
        var data = {};
        if (search) {
          var str = search.substring(1);
          var list = str.split("&");
          list.forEach(function (item) {
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = val;
          });
        }
        return data;
      }
      //渲染页面函数
      function remdem(data) {
        if (status) {
          var {
            pname,
            imgurl,
            nowprice,
            originalcost,
            smallPicList,
            bigPicList,
            num,
          } = data;
          // left
          var showList = "";
          smallPicList.forEach((item, i) => {
            showList += `<li><img src="../${item}" bigUrl="../${bigPicList[i]}" alt=""></li>`;
          });
          $(".wrapAll .left").html(`<div class="showGoods">
                    <img class="smallImg"
                        src="../${bigPicList[0]}"
                        alt="">
                    <div class="shadow"></div>
                </div>
                <ul class="showList">
                    ${showList}
                </ul>`);
          // right
          $(".wrapAll .right").html(`<div class="scaleGoods">
                <img class="bigImg"
                    src="../${bigPicList[0]}"
                    alt="">
            </div>
            <div class="goodsInfo">
                <div class="title">${pname}</div>
                <div class="detail cl">
                    <div class="price">
                        <span>价&emsp;&emsp;格&emsp;</span>
                        <span>￥</span>
                        <span>${nowprice}&nbsp;</span>
                        <span>￥${originalcost}</span>
                    </div>
                    <div>
                        <span>领&emsp;&emsp;券 :&emsp;&emsp;暂时无券  </span>
                    </div>
                    <div class="activity cl">
                        <span>活&emsp;&emsp;动</span>
                        <i>积分抵现</i>
                        <i>此商品匹克积分最高可抵现40元。 </i>
                        <i>积分抵现规则详情>> </i>
                    </div>
                    <div class="activity cl">
                        <span>&emsp;&emsp;&emsp;&emsp;</span>
                        <i>满减包邮</i>
                        <i>【订单满59元】 包邮 </i>
                    </div>
                    <div class="color cl" >
                    </div>
                    <div class="size cl">
                        <p>选择尺寸</p>
                        <li>S</li>
                    </div>
                    <div class="num cl">
                        <p>购买数量</p>
                        <div class="count cl">
                            <div class="reduce">
                                <a href="javascript:;">-</a>
                            </div>
                            <input type="text" class="count-input" value="1">
                            <div class="add">
                                <a href="javascript:;">+</a>
                            </div>
                        </div>
                        <p>&emsp;件&nbsp;（库存${num}件）</p>
                    </div>
                    <div class="buyGoods">
                        <div class="addToShoppingCar">
                            <img src="../images/shoppingcart.png" alt="">
                            <span>加入购物车</span>
                        </div>
                        <div class="heart">
                            <img src="../images/heart.png" alt="">
                            <span>喜欢</span>
                        </div>
                    </div>
                </div>
            </div>`);
          // $(".goodsDetail").html(goodsDetail);
        }
      }
      //动态生成
      var pid = urlParse().pid;
      // console.log(pid);

      if (pid) {
        var result = await goodDetail({ pid });
        var { status, msg, data } = result;
        // console.log(data);
        remdem(data); //初始化页面
      }
      //点击切换图片
      $(document).on("click", ".showList li", function (e) {
        var bigUrl = $(this).find("img").attr("bigUrl");
        $(".smallImg,.bigImg").prop("src", bigUrl);
      });
      //选择尺寸
      $(".size li").click(function () {
        $(this).css("border", "1px solid red");
      });
      //购物车+ -商品 php中进行查询 如果有该产品数据在cart表中 updata  如果没有 添加数据
      $(".reduce").click(function () {
        var num = $(this).next().val();
        num--;
        if (num <= 1) {
          num = 1;
        }
        $(this).next().val(num);
      });
      $(".add").click(function () {
        // console.log(111);
        var num = $(this).prev().val();
        num++;
        $(this).prev().val(num);
      });

      $(".addToShoppingCar").on("click", async function () {
        if (username) {
          var buyNum = $(".count-input").val();
          console.log(buyNum);
          var result = await addToShoppingCar({ username, pid, buyNum });
          var { status, msg } = result;
          console.log(result);
          if (status) {
            if (confirm("商品添加成功,是否进入购物车?")) {
              location.href = "shoppingCar.html";
            }
          } else {
            alert(msg);
          }
        } else {
          location.href =
            "login.html?returnUrl=" + encodeURIComponent(location.href);
        }
      });

      //放大镜
      $(".smallImg")
        .mouseenter(function () {
          $(".shadow").show();
          $(".scaleGoods").show();
        })
        .mouseleave(function () {
          $(".shadow").hide();
          $(".scaleGoods").hide();
        })
        .mousemove(function (e) {
          var x = e.pageX - $(".smallImg").position().left - 200;
          var y = e.pageY - $(".smallImg").position().top - 200;
          if (x <= 0) {
            x = 0;
          }
          if (x > 200) {
            x = 200;
          }
          if (y <= 0) {
            y = 0;
          }
          if (y > 200) {
            y = 200;
          }
          $(".shadow").css({
            left: x,
            top: y,
          });

          $(".scaleGoods img").css({
            left: -x * 2,
            top: -y * 2,
          });
        });
    });
  </script>
</html>
